package com.ly.imp;

import com.ly.rule.LyRequest;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.QueryStringDecoder;

import java.util.List;
import java.util.Map;

/**
 * 既然JavaEE中有对lyServlet规范的定义，那么lycat的工程师来实现这个规范
 *
 * @author liuyang
 */
public class LyHttpRequest implements LyRequest {

    /**
     * 我们在处理底层通信数据的时候，选用了netty这款通信框架，
     * 所以我们通过构造函数传递进来,从HttpRequest中获取通信数据
     */
    private HttpRequest request;

    public LyHttpRequest(HttpRequest request) {
        this.request = request;
    }

    @Override
    public String getUri() {
        return this.request.uri();
    }

    @Override
    public String getPath() {
        QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
        return decoder.path();
    }

    @Override
    public String getMethod() {
        return request.method().name();
    }

    @Override
    public Map<String, List<String>> getParameters() {
        QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
        return decoder.parameters();
    }

    @Override
    public List<String> getParameters(String name) {
        return getParameters().get(name);
    }
}
